package com.guhappy.action.contest;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.guhappy.action.model.ListValue;
import com.guhappy.model.contest.ContestPlayer;
import com.guhappy.model.contest.ContestPlayerHome;
import com.guhappy.model.contest.ContestStock;
import com.guhappy.model.contest.ContestStockHome;
import com.guhappy.model.user.CustomerHome;
import com.guhappy.model.user.GlobalStock;
import com.guhappy.model.user.GlobalStockHome;
import com.guhappy.model.user.WebUser;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("default")
public class JoinContest extends ActionSupport implements ServletRequestAware
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List contestlist = new ArrayList<ListValue>();
	private String area;
	private String stock ;
	private String contesttype ="1";
	private String direction = "up";
	HttpServletRequest request;
	
	@Action(value="joinContest", results={@Result(name="success", location="/pages/joinContest.jsp")})
	public String execute() throws Exception {
		if (null != request.getSession().getAttribute("contestList"))
			contestlist = (List) request.getSession().getAttribute("contestList");
		return SUCCESS;
	}
	
	@Action(value="pay", results={@Result(name="success", location="/pages/payPal.jsp")})
	public String payPalAction() {
		if (null != request.getSession().getAttribute("contestList"))
			contestlist = (List) request.getSession().getAttribute("contestList");
		// save the list to the table
		ContestPlayer cp = new ContestPlayer();
		cp.setCustomer(CustomerHome.findById(((WebUser)request.getSession().getAttribute("webUser")).getId()));
		Iterator it = contestlist.iterator();
		while(it.hasNext()){
			GlobalStock gst =  GlobalStockHome.findByTicker(((ListValue)it.next()).getMyValue());
			ContestStock st = new ContestStock();
			st.setContestPlayer(cp);
			st.setStock(gst);
			cp.getContStockList().add(st);
			ContestStockHome.attachDirty(st);
		}
		ContestPlayerHome.attachDirty(cp);
		area = null;
		stock = null;
		return SUCCESS;
	}
	
	@Action(value="nextPick", results={@Result(name="success", location="/pages/joinContest.jsp")})
	public String nextPick() {
		if (null != request.getSession().getAttribute("contestList"))
			contestlist = (List) request.getSession().getAttribute("contestList");
		contestlist.add(new ListValue(contesttype+direction,stock));
		request.getSession().setAttribute("contestList",contestlist);
		area = null;
		stock = null;
		return SUCCESS;
	}
	public List getContestlist() {
		return contestlist;
	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}

	public String getStock() {
		return stock;
	}

	public void setStock(String stock) {
		this.stock = stock;
	}

	public String getContesttype() {
		return contesttype;
	}

	public void setContesttype(String contesttype) {
		this.contesttype = contesttype;
	}

	public String getDirection() {
		return direction;
	}

	public void setDirection(String direction) {
		this.direction = direction;
	}

	public void setContestlist(List contestlist) {
		this.contestlist = contestlist;
	}

	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}

}
